namespace DotNetNuke.Services.FileSystem
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Runtime.CompilerServices;

    /// -----------------------------------------------------------------------------
    /// Project	 : DotNetNuke
    /// Class	 : FileController
    /// 
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Business Class that provides access to the Database for the functions within the calling classes
    /// Instantiates the instance of the DataProvider and returns the object, if any
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [DYNST]	2/1/2004	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class FileController
    {
        public int AddFile(DotNetNuke.Services.FileSystem.FileInfo file, string folder)
        {
            return this.AddFile(file.PortalId, file.FileName, file.Extension, (long) file.Size, file.Width, file.Height, file.ContentType, folder);
        }

        public int AddFile(int PortalId, string FileName, string Extension, long Size, int Width, int Height, string ContentType, string Folder)
        {
            return this.AddFile(PortalId, FileName, Extension, Size, Width, Height, ContentType, Folder, true);
        }

        public int AddFile(int PortalId, string FileName, string Extension, long Size, int Width, int Height, string ContentType, string Folder, bool ClearCache)
        {
            int AddFile;
            bool IsDirty = false;
            try
            {
                int FileId;
                DataRow[] dr = this.GetAllFiles().Select("FileName='" + FileName + "' and PortalId " + Interaction.IIf(PortalId == Null.NullInteger, "IS NULL", "=" + PortalId.ToString()).ToString() + " and Folder='" + Folder + "'");
                if (dr.Length > 0)
                {
                    DataRow OriginalFile = dr[0];
                    FileId = Convert.ToInt32(RuntimeHelpers.GetObjectValue(OriginalFile["FileId"]));
                    if (this.FileChanged(OriginalFile, FileName, Extension, Size, Width, Height, ContentType, Folder))
                    {
                        DataProvider.Instance().UpdateFile(FileId, FileName, Extension, Size, Width, Height, ContentType, Folder);
                        IsDirty = true;
                    }
                }
                else
                {
                    FileId = DataProvider.Instance().AddFile(PortalId, FileName, Extension, Size, Width, Height, ContentType, Folder);
                    IsDirty = true;
                }
                AddFile = FileId;
            }
            finally
            {
                if (IsDirty & ClearCache)
                {
                    this.GetAllFilesRemoveCache();
                }
            }
            return AddFile;
        }

        public int ConvertFilePathToFileId(string FilePath, int PortalID)
        {
            string FileName = "";
            string FolderName = "";
            int FileId = -1;
            if (FilePath != "")
            {
                FileName = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
                FolderName = FilePath.Replace(FileName, "");
            }
            DotNetNuke.Services.FileSystem.FileInfo objFile = new FileController().GetFile(FileName, PortalID, FolderName);
            if (objFile != null)
            {
                FileId = objFile.FileId;
            }
            return FileId;
        }

        public void DeleteFile(int PortalId, string FileName, string folder)
        {
            this.DeleteFile(PortalId, FileName, folder, true);
        }

        public void DeleteFile(int PortalId, string FileName, string folder, bool ClearCache)
        {
            DataProvider.Instance().DeleteFile(PortalId, FileName, folder);
            if (ClearCache)
            {
                this.GetAllFilesRemoveCache();
            }
        }

        public void DeleteFiles(int PortalId)
        {
            this.DeleteFiles(PortalId, true);
        }

        public void DeleteFiles(int PortalId, bool ClearCache)
        {
            DataProvider.Instance().DeleteFiles(PortalId);
            if (ClearCache)
            {
                this.GetAllFilesRemoveCache();
            }
        }

        internal bool FileChanged(DataRow drOriginalFile, string NewFileName, string NewExtension, long NewSize, int NewWidth, int NewHeight, string NewContentType, string NewFolder)
        {
            return (((((((Convert.ToString(RuntimeHelpers.GetObjectValue(drOriginalFile["FileName"])) != NewFileName) | (Convert.ToString(RuntimeHelpers.GetObjectValue(drOriginalFile["Extension"])) != NewExtension)) | (Convert.ToInt32(RuntimeHelpers.GetObjectValue(drOriginalFile["Size"])) != NewSize)) | (Convert.ToInt32(RuntimeHelpers.GetObjectValue(drOriginalFile["Width"])) != NewWidth)) | (Convert.ToInt32(RuntimeHelpers.GetObjectValue(drOriginalFile["Height"])) != NewHeight)) | (Convert.ToString(RuntimeHelpers.GetObjectValue(drOriginalFile["ContentType"])) != NewContentType)) | (Convert.ToString(RuntimeHelpers.GetObjectValue(drOriginalFile["Folder"])) != NewFolder));
        }

        public DataTable GetAllFiles()
        {
            DataTable dt;
            if (DataCache.GetCache("GetAllFiles") == null)
            {
                dt = DataProvider.Instance().GetAllFiles();
                DataCache.SetCache("GetAllFiles", dt);
            }
            else
            {
                dt = (DataTable) DataCache.GetCache("GetAllFiles");
            }
            if (dt != null)
            {
                return dt.Copy();
            }
            return new DataTable();
        }

        public void GetAllFilesRemoveCache()
        {
            DataCache.RemoveCache("GetAllFiles");
        }

        public DotNetNuke.Services.FileSystem.FileInfo GetFile(string FilePath, int PortalId)
        {
            return this.GetFile(Path.GetFileName(FilePath), PortalId, FilePath.Replace(Path.GetFileName(FilePath), ""));
        }

        public DotNetNuke.Services.FileSystem.FileInfo GetFile(string FileName, int PortalId, string Folder)
        {
            return (DotNetNuke.Services.FileSystem.FileInfo) CBO.FillObject(DataProvider.Instance().GetFile(FileName, PortalId, Folder), typeof(DotNetNuke.Services.FileSystem.FileInfo));
        }

        public DotNetNuke.Services.FileSystem.FileInfo GetFileById(int FileId, int PortalId)
        {
            return (DotNetNuke.Services.FileSystem.FileInfo) CBO.FillObject(DataProvider.Instance().GetFileById(FileId, PortalId), typeof(DotNetNuke.Services.FileSystem.FileInfo));
        }

        public IDataReader GetFiles(int PortalId, string Folder)
        {
            return DataProvider.Instance().GetFiles(PortalId, Folder);
        }

        public ArrayList GetFilesByFolder(int PortalId, string Folder)
        {
            return CBO.FillCollection(DataProvider.Instance().GetFiles(PortalId, Folder), typeof(DotNetNuke.Services.FileSystem.FileInfo));
        }

        public void UpdateFile(int PortalId, string OriginalFileName, string FileName, string Extension, long Size, int Width, int Height, string ContentType, string SourceFolder, string DestinationFolder)
        {
            this.UpdateFile(PortalId, OriginalFileName, FileName, Extension, Size, Width, Height, ContentType, SourceFolder, DestinationFolder, true);
        }

        public void UpdateFile(int PortalId, string OriginalFileName, string FileName, string Extension, long Size, int Width, int Height, string ContentType, string SourceFolder, string DestinationFolder, bool ClearCache)
        {
            bool IsDirty = false;
            try
            {
                DataRow[] dr = this.GetAllFiles().Select("FileName='" + OriginalFileName + "' and PortalId" + Interaction.IIf(PortalId == Null.NullInteger, "IS NULL", "=" + PortalId.ToString()).ToString() + " and Folder='" + SourceFolder + "'");
                if (dr.Length > 0)
                {
                    DataRow OriginalFile = dr[0];
                    int FileId = Convert.ToInt32(RuntimeHelpers.GetObjectValue(OriginalFile["FileId"]));
                    if (this.FileChanged(OriginalFile, FileName, Extension, Size, Width, Height, ContentType, DestinationFolder))
                    {
                        DataProvider.Instance().UpdateFile(FileId, FileName, Extension, Size, Width, Height, ContentType, DestinationFolder);
                        IsDirty = true;
                    }
                }
            }
            finally
            {
                if (IsDirty & ClearCache)
                {
                    this.GetAllFilesRemoveCache();
                }
            }
        }
    }
}

